package kr.inani.android;

import kr.inani.R;
import kr.inani.android.search.BookItem;
import kr.inani.android.view.ProfileImageView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class BookDetailActivity extends Activity implements OnClickListener
{
	private BookItem bookItem;
	private boolean enableDelete;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.book_detail);
		
		bookItem = getIntent().getExtras().getParcelable(BookItem.BOOK_ITEM_STRING);
		enableDelete = getIntent().getBooleanExtra("enableDelete", false);
		((ProfileImageView) findViewById(R.id.bookDetailImg)).setImageBitmap(bookItem.getBitmap());
		((TextView) findViewById(R.id.bookDetailTitle)).setText( Html.fromHtml(bookItem.getTitle()) );	
		((TextView) findViewById(R.id.bookDetailText)).setText(makeDetailString());
		((TextView) findViewById(R.id.bookDetailSummary)).setText(makeSummaryString());
		((TextView) findViewById(R.id.bookDetailLink)).setText(makeLinkString());
		setMemo();
		setupDeleteButton();
		setupSaveButton();
	}
	
	@Override
	public void onClick(View v)
	{
		Intent intent = new Intent();
		intent.putExtra(BookItem.BOOK_ITEM_STRING, bookItem);
		setResult(RESULT_OK, intent);
		if(v.getId()==R.id.saveBookMemo) {
			String memo = ((TextView) findViewById(R.id.bookDetailMemo)).getText().toString();
			if(!memo.equals(bookItem.getMemo())) {
				bookItem.setMemo(memo);
				intent.putExtra("changeMemo", true);
			}
		}
		finish();
	}

	
	////////////////////// private /////////////////////////

	private SpannableStringBuilder makeDetailString()
	{
		SpannableStringBuilder strBuilder = new SpannableStringBuilder();
		strBuilder.append(getString(R.string.author)+" : "+Html.fromHtml(bookItem.getAuthor()));
		strBuilder.append("\n");
		strBuilder.append(getString(R.string.price)+" : "+bookItem.getPrice());
		strBuilder.append("\n");
		strBuilder.append(getString(R.string.sale_price)+" : "+bookItem.getDiscount());
		strBuilder.append("\n");
		strBuilder.append(getString(R.string.publisher)+" : "+Html.fromHtml(bookItem.getPublisher()));
		strBuilder.append("\n");
		strBuilder.append(getString(R.string.publish_date)+" : "+bookItem.getPubdate());
		strBuilder.append("\n");
		strBuilder.append("ISBN : "+Html.fromHtml(bookItem.getIsbn()));
		return strBuilder;
	}
	
	private SpannableStringBuilder makeSummaryString()
	{
		SpannableStringBuilder summaryBuilder = new SpannableStringBuilder(getString(R.string.summary));
		summaryBuilder.append(" : ");
		summaryBuilder.append("\n");
		summaryBuilder.append(Html.fromHtml(bookItem.getDescription()));
		return summaryBuilder;
	}

	private SpannableStringBuilder makeLinkString()
	{
		SpannableStringBuilder linkBuilder = new SpannableStringBuilder(getString(R.string.link));
		linkBuilder.append(" : ");
		linkBuilder.append("\n");
		linkBuilder.append(bookItem.getLinkUrl());
		return linkBuilder;
	}

	private void setupDeleteButton()
	{
		Button deleteButton = (Button) findViewById(R.id.deleteBook);
		if(enableDelete) {
			deleteButton.setVisibility(View.VISIBLE);
			deleteButton.setOnClickListener(this);
		} else {
			deleteButton.setVisibility(View.GONE);
		}
	}
	
	private void setupSaveButton()
	{
		Button saveButton = (Button) findViewById(R.id.saveBookMemo);
		if(enableDelete) {
			saveButton.setVisibility(View.VISIBLE);
			saveButton.setOnClickListener(this);
		} else {
			saveButton.setVisibility(View.GONE);
		}
	}
	
	
	private void setMemo()
	{
		if(enableDelete) {
			((EditText) findViewById(R.id.bookDetailMemo)).setVisibility(View.VISIBLE);
			((EditText) findViewById(R.id.bookDetailMemo)).setText(bookItem.getMemo());
		} else {
			((EditText) findViewById(R.id.bookDetailMemo)).setVisibility(View.GONE);
		}
	}
}
